import { Pool } from "../../app_base/base/pool/Pool";
import { IViewLoader, ViewConfig } from "../../app_base/module/view/IViewLoader";
import { IViewManager } from "../../app_base/module/view/ViewManager";
import { CCPViewLoader } from "./CCPViewLoader";

export class CocosViewManager implements IViewManager {
    private _loaderPoolMap: Map<string, Pool<IViewLoader>> = new Map();
    /**
     * 从loader池中获取loader
     * @param loaderType 
     * @returns 
     */
    getLoader(config: ViewConfig): IViewLoader {
        let pool = this._getLoaderPool(config.loaderType || CCPViewLoader.TYPE);
        return pool.allocate(pool);
    }

    private _getLoaderPool(loaderType: string = CCPViewLoader.TYPE): Pool<IViewLoader> {
        let pool: Pool<IViewLoader> = this._loaderPoolMap.get(loaderType);
        let loaderClass;
        switch (loaderType) {
            case CCPViewLoader.TYPE:
                loaderClass = CCPViewLoader;
                break;
            default:
                loaderClass = CCPViewLoader;
                break;
        }
        if (!pool) {
            pool = new Pool(loaderClass);
            this._loaderPoolMap.set(loaderType, pool);
        }
        return pool;
    }
}
